const router = require('koa-router')()
const {
	getList,
	getDetail,
	newBlog,
	updateBlog,
	delBlog
} = require('../../../../common/controller/blog')
const loginCheck = require('../middleware/login-check')
const {
	ErrorModel,
	SuccessModel
} = require('../../../../common/model/res-model')
router.prefix('/api/blog')

router.get('/list', async (ctx, next) => {
	let author = ctx.query.author
	const keyword = ctx.query.keyword

	if (ctx.query.isadmin) {
		console.log('is admin')
		if (!ctx.session.username) {
			return (ctx.body = new ErrorModel('未登录'))
		}
		author = ctx.session.username
	}

	const listData = await getList(author, keyword)
	ctx.body = new SuccessModel(listData)
})

router.get('/detail', async (ctx, next) => {
	const data = await getDetail(ctx.query.id)
	ctx.body = new SuccessModel(data)
})

router.post('/new', loginCheck, async (ctx, next) => {
	const body = ctx.request.body
	body.author = ctx.session.username
	const data = await newBlog(body)
	ctx.body = new SuccessModel(data)
})

router.post('/update', loginCheck, async (ctx, next) => {
	const result = await updateBlog(ctx.query.id, ctx.request.body)
	if (result) {
		ctx.body = new SuccessModel()
	} else {
		ctx.body = new ErrorModel('更新博客失败')
	}
})

router.post('/del', loginCheck, async (ctx, next) => {
	const author = ctx.session.username
	const result = await delBlog(ctx.query.id, author)
	if (result) {
		ctx.body = new SuccessModel()
	} else {
		ctx.body = new ErrorModel('删除博客失败')
	}
})

module.exports = router
